package org.dromara.web.controller.miniapp;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.lxk.miniapp.config.WxMaConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.system.service.ISysUserService;
import org.dromara.web.service.SysLoginService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信小程序用户接口
 *
 * @author 合一
 */
@Slf4j
@RestController
@RequestMapping("/wx/user/miniapp/{appid}")
@RequiredArgsConstructor
public class WxMaUserController {

    private final SysLoginService loginService;

    private final ISysUserService userService;

    /**
     * 小程序登陆接口
     * @param appid 小程序ID
     * @param code 小程序CODE
     */
//    @SaIgnore
//    @PostMapping("/login")
//    public R<Map<String, Object>> login(@PathVariable String appid, String code) {
//        if (StringUtils.isBlank(code)) {
//            return R.fail("empty jscode");
//        }
//        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
//        try {
//            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
//            log.info("sessionKey:{}, openId:{}, unionId:{}", session.getSessionKey(), session.getOpenid(), session.getUnionid());
//            String token = loginService.xcxLoginByOpenId(session.getOpenid(), session.getUnionid(), UserType.MINI_APP_USER);
//            Map<String, Object> ajax = new HashMap<>();
//            ajax.put(Constants.TOKEN, token);
//            return R.ok(ajax);
//        } catch (WxErrorException e) {
//            log.error(e.getMessage(), e);
//            return R.fail("{wx.miniapp.login.error}");
//        }
//    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @GetMapping("/info")
    public String info(@PathVariable String appid, String sessionKey,
                       String signature, String rawData, String encryptedData, String iv) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }

        // 解密用户信息
        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);

        return JsonUtils.toJsonString(userInfo);
    }

    /**
     * 获取用户绑定手机号信息
     *
     * @param appid
     * @param sessionKey
     * @param signature
     * @param rawData
     * @param encryptedData
     * @param iv
     * @return
     */
    @GetMapping("/phone")
    public String phone(@PathVariable String appid, String sessionKey, String signature,
                        String rawData, String encryptedData, String iv) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }

        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);


        return JsonUtils.toJsonString(phoneNoInfo);
    }

}
